﻿/*  FastExplorer - File System Explorer for Google Desktop
    Copyright (C) 2008 Alexandr Yavorsky 
*/

var fs = new ActiveXObject("Scripting.FileSystemObject");
//var shell = new ActiveXObject("WScript.Shell");

var dirs = new Array();
var dirE = new Array();
var dirL = new Array();

var active = 0;
var tab_count = 1;

var _ident = 5;

var shiftPressed = false;

function refresh()
{
	for (i = 0; i <= tab_count-1; i++)
	{
		dirL[i].RemoveAllElements();
		
		for (j in dirs[i].elements)
		{
			cimg = "images/"+dirs[i].elements[j].type + ".png";

			var _tooltip = "Name: "+dirs[i].elements[j].name;
			if (dirs[i].elements[j].file_size!="") _tooltip += "\nSize: "+dirs[i].elements[j].file_size+"Mb";
			if (dirs[i].elements[j].type=="drive") _tooltip += "\nFree space: "+dirs[i].elements[j].free_space+"Mb";

			var _label = dirs[i].elements[j].name;
			if (dirs[i].elements[j].volume_name!="") _label += " "+dirs[i].elements[j].volume_name;

			dirL[i].appendElement(
				"<listitem onkeyup=\"itmKeyUp()\" tooltip=\"" + _tooltip +
				"\" onmousewheel=\"lbDir_onwheel(" + i + 
				")\" ondblclick=\"lbDir_dblclick(" + i + 
				")\" name=\"" + j + "\">" +
				"<img x=\"1\" y=\"1\" src=\"" + cimg + 
				"\" height=\"20\" width=\"20\" />"+
				"<label color=\"#000000\" x=\"25\" y=\"3\" height=\"20\" width=\"100%\">" + _label+ "</label>" +
				"</listitem>");
		}

		dirE[i].value = dirs[i].location != "" ? dirs[i].location : ROOT_DIR;
		dirL[i].height = dirL[i].itemHeight * dirs[i].elements.length;

		options.putValue("path"+i, dirs[i].location);
	}
	
	view_onSize();
}

function view_onOpen() 
{
	for (i = 0; i <= tab_count-1; i++) {
    options.putDefaultValue("path"+i, "");
		dirs[i] = new Dir(options.getValue("path"+i));
	}
	
	options.putDefaultValue("last_active", "0");

	// !!! Поиск компонента по названию
	dirE[0] = txtPath;
	dirL[0] = lbDir;

	refresh();
}


function txtPath_KeyPressed(n)
{
	if (event.keyCode == 13)
	{
		var newdir = dirE[n].value;

		if (newdir == "" || newdir == ROOT_DIR)
		{
			dirs[n] = new Dir("");
			dirL[n].pinY = 0;		
		}
		else 
		if (fs.FolderExists(newdir))
		{
			dirs[n] = new Dir(newdir);
			dirL[n].pinY = 0;		
		}
		else 
		{
			if (!dirs[n].run(newdir))
				alert(FILE_OPEN_ERROR + " " + newdir + "!");
		}
			
		refresh();

		return false;
	}
}

function lbDir_dblclick(n)
{
	var l = dirs[n].location;

	if (dirL[n].selectedItem == null)
		return;

	dirs[n].navigate(dirL[n].selectedItem.name);

	if (l != dirs[n].location)
		dirL[n].pinY = 0;		

	refresh();
}

function scrollby(n, delta)
{
	if (dirL[n].height <= divDir.height) dirL[n].pinY = 0;
	dirL[n].pinY += delta;
	if (dirL[n].pinY < 0) dirL[n].pinY = 0;
	else 
		if (dirL[n].pinY > dirL[n].height - divDir.height)
			dirL[n].pinY = dirL[n].height - divDir.height;

}

function lbDir_onwheel(n)
{
	scrollby(n, -Event.wheelDelta);
}	

function itmKeyUp()
{
	kc = Event.keyCode;
	li = dirL[active].selectedIndex;
	if (kc == 38) //up
	{
		if (shiftPressed)
		{
			var si;
			for (si = dirL[active].selectedIndex; si < dirL[active].children.count && dirL[active].children.item(si).selected; si++);

			if (si - 1 <= dirL[active].selectedIndex)
				dirL[active].selectedIndex--;
			else if (si < dirL[active].children.count)
				dirL[active].children.item(si-1).selected = false;
		}
		else
			dirL[active].selectedIndex--;
	}
	else if (kc == 33) // page up
	{
		dirL[active].selectedIndex -= 5;
	}
	else if (kc == 36) // home
	{
		dirL[active].selectedIndex = 0;
	}
	else if (kc == 40) // down
	{
		if (shiftPressed)
		{
			var si;
			for (si = dirL[active].selectedIndex; si < dirL[active].children.count && dirL[active].children.item(si).selected; si++);

			if (si - 1 <= dirL[active].selectedIndex)
				dirL[active].selectedIndex++;
			else if (si < dirL[active].children.count)
				dirL[active].children.item(si).selected = true;
		}
		else
			dirL[active].selectedIndex++;
	}
	else if (kc == 34)  // page down
	{
		dirL[active].selectedIndex += 5;
	}
	else if (kc == 35) // end
	{
		dirL[active].selectedIndex = dirL[active].children.count - 1;
	}
	else if (kc == 13) // enter
	{
		lbDir_dblclick(active);
		dirL[active].selectedIndex = 0;
		return;
	}
	else if (kc == 9) // tab
	{
		updateSel(active ^ 1);
		dirL[active].selectedIndex = 0;
		return;
	}
	else if (kc >= 48 && kc <= 90) // alphanumericals
	{
		for (i = 0; i < dirL[active].children.count; i ++)
		{
			if (dirs[active].elements[dirL[active].children.item(i).name].name.toUpperCase().charCodeAt(0) == kc)
			{
				dirL[active].selectedIndex = i;
				break;
			}
		}
	}
	else
	{
		Event.returnValue = true;
		return;
	}

	if (dirL[active].selectedIndex < 0)
	{
		if (li > dirL[active].children.count / 2)
			dirL[active].selectedIndex = dirL[active].children.count - 1;
		else
			dirL[active].selectedIndex = 0;
	}

	while (dirL[active].selectedIndex * dirL[active].itemHeight < dirL[active].pinY)
		scrollby(active, -30);
	while (dirL[active].selectedIndex * dirL[active].itemHeight > dirL[active].pinY + 150)
		scrollby(active, 30);
	
	Event.returnValue = false;
	return;	
}

function view_keyDown()
{
	if (Event.keyCode == 16)
		shiftPressed = true;
}

function view_keyUp()
{
	if (Event.keyCode == 16)
		shiftPressed = false;
}

function numSelected()
{
	var si;
	for (si = dirL[active].selectedIndex; si < dirL[active].children.count && dirL[active].children.item(si).selected; si++);
	return si - dirL[active].selectedIndex;
}

function view_onSize() {
	txtPath.x = _ident;
	txtPath.y = _ident;
	txtPath.Width = view.Width - 2 * _ident;
  
	divDir.x = _ident;
	divDir.y = 2 * _ident + txtPath.Height;
  divDir.Height = view.Height - divDir.y - _ident;
	divDir.Width = view.Width - 2 * _ident;
}

